package com.yiren.manager.ctrl.messhall;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrStaffInfo;
import com.yiren.entity.YrWithdrawApply;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.messhall.YrWithdrawApplyForm;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.messhall.YrWithdrawApplyService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("withdrawApply")
@Controller
public class YrWithdrawApplyController extends GenericController<YrWithdrawApplyForm, YrWithdrawApply>{

	@Autowired
	private YrWithdrawApplyService selfService; 
	
	@Autowired
	private YrStaffInfoService staffInfoService;
	
	@RequestMapping("save")
	@ResponseBody
	public HTTPResult<YrWithdrawApplyForm> save(String data) throws MyException, SwallowException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrWithdrawApplyForm> result = new HTTPResult<YrWithdrawApplyForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrWithdrawApplyForm form = tool.mapper("withdrawApply", YrWithdrawApplyForm.class);
		if(null==form){
			throwExp("param.error");
		}

		String sqlWhere = "accounts_month=?1";
		QueryResult<YrWithdrawApply> qr = selfService.find(YrWithdrawApply.class, sqlWhere, new Object[]{form.getAccountsMonth()});
		
		if(qr.getTotalRecord()>0){
			throwExp("该月份已提交过申请或已被处理，请勿重复申请");
		}
		
		if(null==form.getWithdrawApplyId()||form.getWithdrawApplyId().isEmpty()){
			form.setWithdrawApplyId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
			form.setWithdrawWay(-1);
			form.setHandlerStatus(0);
			form.setApplyDate(new Date());
		}
		form.setUpdateTime(new Date());
		YrWithdrawApply entity = ObjectUtils.objectToObject(form, YrWithdrawApply.class);
		selfService.save(entity);
		
		return result;		
	}
	
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrWithdrawApplyForm> findAll() throws Exception{
		HTTPResult<YrWithdrawApplyForm> result = new HTTPResult<YrWithdrawApplyForm>();
		String sqlWhere = "handler_status=0";
		
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("applyDate", "ASC");
		
		QueryResult<YrWithdrawApply> qr = selfService.find(YrWithdrawApply.class, sqlWhere, new Object[]{},map);
		
		List<YrWithdrawApplyForm> formList = new ArrayList<YrWithdrawApplyForm>();
		for(YrWithdrawApply entity : qr.getResultList()){
			YrWithdrawApplyForm form = toForm(entity);
			YrStaffInfo staffInfo = staffInfoService.findOne(form.getApplyId());
			if(staffInfo!=null){
				form.setApplyer(staffInfo.getName());
			}
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	/**
	 * 处理提现申请
	 * @param id
	 * @param withdrawWay
	 * @return
	 * @throws MyException
	 */
	@RequestMapping("handleApply")
	@ResponseBody
	public HTTPResult<YrWithdrawApplyForm> handleApply(String id,Integer withdrawWay) throws MyException{
		HTTPResult<YrWithdrawApplyForm> result = new HTTPResult<YrWithdrawApplyForm>();
		
		YrWithdrawApply entity = selfService.findOne(id);
		if(null==entity){
			throwExp("提现申请不存在");
		}
		
		entity.setHandlerStatus(1);//设置为已处理状态
		entity.setHandleDate(new Date());
		entity.setWithdrawWay(withdrawWay);
		
		selfService.save(entity);
		
		return result;
	}
	
	@RequestMapping("findRecordByMonth")
	@ResponseBody
	public HTTPResult<YrWithdrawApplyForm> findRecordByMonth(String month) throws Exception{
		HTTPResult<YrWithdrawApplyForm> result = new HTTPResult<YrWithdrawApplyForm>();
		String sqlWhere = "DATE_FORMAT(withdraw_date,'%Y-%m') = ?1 AND handler_status=1";
	
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("withdrawDate", "ASC");
		
		QueryResult<YrWithdrawApply> qr = selfService.find(YrWithdrawApply.class, sqlWhere, new Object[]{month},map);
		
		List<YrWithdrawApplyForm> formList = new ArrayList<YrWithdrawApplyForm>();
		for(YrWithdrawApply entity : qr.getResultList()){
			YrWithdrawApplyForm form = toForm(entity);
			YrStaffInfo staffInfo = staffInfoService.findOne(form.getApplyId());
			if(staffInfo!=null){
				form.setApplyer(staffInfo.getName());
			}
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	
	@Override
	public YrWithdrawApplyForm toForm(YrWithdrawApply e) throws Exception {
		YrWithdrawApplyForm form = ObjectUtils.objectToObject(e, YrWithdrawApplyForm.class);
		return form;
	}

	@Override
	public YrWithdrawApply toEntity(YrWithdrawApplyForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
